package com.cp_smss.web;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cp_smss.common.GlobalConstants;
import com.cp_smss.model.DWZResponser;
import com.cp_smss.model.Page;
import com.cp_smss.model.Project;
import com.cp_smss.model.User;
import com.cp_smss.service.BusinessService;
import com.cp_smss.util.DWZResponseFactory;

@Controller
@RequestMapping(value="/business/")
public class BusinessController {
	
	@Resource(name="businessService")
	private BusinessService businessService;
	
	@RequestMapping(value="/list")
	public String list(User user, Page page,ModelMap modelMap){
		List<Project> list = businessService.listProjects(page);
		modelMap.put("list", list);
		modelMap.put("page", page);
		return "list";
	}
	
	@RequestMapping(value="/del")
	@ResponseBody
	public Object del(int[] pids){
		
		businessService.deleteProject(pids);
		
		DWZResponser dwzResponser = DWZResponseFactory.create();
		dwzResponser.setStatusCode(GlobalConstants.AJAX_SUCCESS);
		dwzResponser.setMessage(GlobalConstants.SUCCESS_OPERATE);
		dwzResponser.setNavTabId("list");
		return dwzResponser;
	}
	
	@RequestMapping(value="add")
	@ResponseBody
	public Object addProject(Project project){
		User user =new User();
		user.setId(1);
		project.setUploader(user);
		int result = businessService.addProject(project);
		
		DWZResponser dwzResponser = DWZResponseFactory.create();
		dwzResponser.setStatusCode(1==result?GlobalConstants.AJAX_SUCCESS:GlobalConstants.AJAX_FAIL);
		dwzResponser.setMessage(GlobalConstants.SUCCESS_OPERATE);
		dwzResponser.setCallbackType(GlobalConstants.AJAX_CALLBACK_CLOSE_CURRENT);
		dwzResponser.setNavTabId("list");
		return dwzResponser;
	}
	
	@RequestMapping(value="edit")
	@ResponseBody
	public DWZResponser editProject(Project project){
		
		businessService.editProject(project);
		
		DWZResponser dwzResponser = DWZResponseFactory.create();
		dwzResponser.setStatusCode(GlobalConstants.AJAX_SUCCESS);
		dwzResponser.setMessage(GlobalConstants.SUCCESS_OPERATE);
		dwzResponser.setCallbackType(GlobalConstants.AJAX_CALLBACK_CLOSE_CURRENT);
		dwzResponser.setNavTabId("list");
		return dwzResponser;
	}
}
